package fr.upstlse.glre.client.espacededie.commun.communiquer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.SuggestBox;

import fr.upstlse.glre.client.administration.vitrine.editeurDePage.RTT.RichTextToolbar;
import fr.upstlse.glre.client.constante.fields.MessageConstante;
import fr.upstlse.glre.client.utilitaires.ControlerUtil;
import fr.upstlse.glre.client.vo.communiquer.MessageVOGWT;

public class Ecrire extends LayoutContainer {

	SuggestBox aSuggestBox;
	TextField<String> objetTextField;
	RichTextArea richTextArea;
	Button envoyerButtonHaut;
	Button envoyerButtonBas;
	
	public Ecrire() {
		
	}

	@Override
	protected void onRender(Element pParent, int pIndex) {
		super.onRender(pParent, pIndex);
		
		HorizontalPanel lHorizontalPanel = new HorizontalPanel();
		lHorizontalPanel.add(new Html(MessageConstante.LABEL_A_TEXT.getValue()));
		EmailSuggestOracle lEmailSuggestOracle = new EmailSuggestOracle();
		aSuggestBox = new SuggestBox(lEmailSuggestOracle);
		aSuggestBox.setHeight("50px");
		aSuggestBox.setWidth("200%");
		aSuggestBox.addStyleName("x-form-text");
		lHorizontalPanel.add(aSuggestBox);
		
		objetTextField = new TextField<String>();
		objetTextField.setId(MessageConstante.LABEL_OBJET_NAME.getValue());
		objetTextField.setFieldLabel(MessageConstante.LABEL_OBJET_TEXT.getValue());
		objetTextField.setEmptyText(MessageConstante.LABEL_OBJET_TEXT.getValue());
		objetTextField.setWidth("100%");
		
		envoyerButtonHaut = createEnvoyerButton();
		envoyerButtonBas = createEnvoyerButton();
		
		richTextArea = new RichTextArea();
		richTextArea.setSize("100%", "14em");
	    RichTextToolbar lToolbar = new RichTextToolbar(richTextArea);
	    lToolbar.ensureDebugId("cwRichText-toolbar");
	    lToolbar.setWidth("100%");

	    // Add the components to a panel
	    Grid lGrid = new Grid(6, 1);
	    lGrid.setStyleName("cw-RichText");
	    lGrid.setWidget(0, 0, envoyerButtonHaut);
	    lGrid.setWidget(1, 0, lHorizontalPanel);
	    lGrid.setWidget(2, 0, objetTextField);
	    lGrid.setWidget(3, 0, lToolbar);
	    lGrid.setWidget(4, 0, richTextArea);
	    lGrid.setWidget(5, 0, envoyerButtonBas);
	    
	    add(lGrid);
	}
	
	protected Button createEnvoyerButton() {
		Button lEnvoyerButton = new Button();
		lEnvoyerButton.setText(MessageConstante.BUTTON_ENVOYER_TEXT.getValue());
		lEnvoyerButton.addSelectionListener(new SendButtonListener());
		return lEnvoyerButton;
	}
	
	private List<String> getDestinataires(String pListe) {
		String[] lDestinataireTab = pListe.split(",");
		List<String> lDestinataires = new ArrayList<String>(lDestinataireTab.length);
		for(String lDestinaire : lDestinataireTab) {
			lDestinataires.add(lDestinaire);
		}
		return lDestinataires;
	}
	
	protected class SendButtonListener extends SelectionListener<ComponentEvent> {

		@Override
		public void componentSelected(ComponentEvent pCe) {
			MessageVOGWT lMessageVOGWT = new MessageVOGWT();
			lMessageVOGWT.setTitre(objetTextField.getValue());
			lMessageVOGWT.setContenu(richTextArea.getHTML());
			lMessageVOGWT.setDate(new Date());
			lMessageVOGWT.setExpediteur(Message.getInstance().getUserVOGWT());
			List<String> lDestinataires = getDestinataires(aSuggestBox.getText());
			final MessageBox lAttenteMessageBox = MessageBox.wait("Chargement", "Envoi en cours...", "Veuillez patienter...");
			AsyncCallback<Object> lAsyncCallback = new AsyncCallback<Object>() {

				public void onFailure(Throwable pArg0) {
					lAttenteMessageBox.hide();
					MessageBox.alert("Erreur envoyer ", pArg0.getMessage(),
							null);
				}

				public void onSuccess(Object pArg0) {
					lAttenteMessageBox.hide();
					Info.display("Information",
							"Envoyé avec succès", "");
				}
			};
			ControlerUtil.getUserControlerServiceAsyncInstance().envoyer(lMessageVOGWT, lDestinataires, lAsyncCallback);
		}
	}

	public SuggestBox getASuggestBox() {
		return aSuggestBox;
	}

	public void setASuggestBox(SuggestBox pSuggestBox) {
		aSuggestBox = pSuggestBox;
	}

	public TextField<String> getObjetTextField() {
		return objetTextField;
	}

	public void setObjetTextField(TextField<String> pObjetTextField) {
		objetTextField = pObjetTextField;
	}

	public RichTextArea getRichTextArea() {
		return richTextArea;
	}

	public void setRichTextArea(RichTextArea pRichTextArea) {
		richTextArea = pRichTextArea;
	}

	public Button getEnvoyerButtonHaut() {
		return envoyerButtonHaut;
	}

	public void setEnvoyerButtonHaut(Button pEnvoyerButtonHaut) {
		envoyerButtonHaut = pEnvoyerButtonHaut;
	}

	public Button getEnvoyerButtonBas() {
		return envoyerButtonBas;
	}

	public void setEnvoyerButtonBas(Button pEnvoyerButtonBas) {
		envoyerButtonBas = pEnvoyerButtonBas;
	}
}
